package sentinel.demo.test;

import org.redisson.Redisson;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTest8 {

    public static void main(String[] args) throws InterruptedException {
        Config config=new Config();
        config.useSingleServer().setAddress("redis://192.168.8.38:6379").setPassword("Jxkj@2022");
        RedissonClient client= Redisson.create(config);

        RRateLimiter rateLimiter=client.getRateLimiter("rate_limiter");
        rateLimiter.trySetRate(RateType.PER_CLIENT,5,2, RateIntervalUnit.SECONDS);

        ExecutorService executorService= Executors.newFixedThreadPool(10);
        for (int i=0;i<10;i++){
            if (i==8){
                Thread.sleep(2000);
            }
            executorService.submit(()->{
               try{
                   boolean b = rateLimiter.tryAcquire();
                   if (b){
                       System.out.println("线程"+Thread.currentThread().getId()+"进入数据区："+System.currentTimeMillis());
                   }
               }catch (Exception e){
                   e.printStackTrace();
               }
            });
        }
        executorService.shutdown();
    }
}
